package com.accessibleworlds.user;

import com.accessibleworlds.util.SharedVariables;
import com.accessibleworlds.R;

import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;

public class Options extends PreferenceActivity{
	
	private ListPreference language;
	private CheckBoxPreference autoLogin,blindUserMode,otherLanguages;
	private EditTextPreference email,password;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.options);
        
        initializeVariables();
        changeLanguage();
	}
	
	@Override
	public void onBackPressed() {
		finish();
		overridePendingTransition(R.anim.fade, R.anim.hold);
	}
	
	private void initializeVariables() {
		language = (ListPreference)findPreference("language");
		language.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				SharedVariables.currentLanguage = Integer.parseInt(newValue.toString());
				changeLanguage();
				return true;
			}
		});
		autoLogin = (CheckBoxPreference)findPreference("autoLogin");
		email = (EditTextPreference)findPreference("email");
		password = (EditTextPreference)findPreference("password");
		blindUserMode = (CheckBoxPreference)findPreference("blindUserMode");
		otherLanguages = (CheckBoxPreference)findPreference("otherLanguages");
	}
	
	private void changeLanguage() {
		language.setTitle(getResources().getStringArray(R.array.language)[SharedVariables.currentLanguage]);
		language.setSummary(getResources().getStringArray(R.array.languageSummary)[SharedVariables.currentLanguage]);
		language.setDialogTitle(getResources().getStringArray(R.array.language)[SharedVariables.currentLanguage]);
		autoLogin.setTitle(getResources().getStringArray(R.array.autoLogin)[SharedVariables.currentLanguage]);
		email.setTitle(getResources().getStringArray(R.array.email)[SharedVariables.currentLanguage]);
		email.setDialogTitle(getResources().getStringArray(R.array.email)[SharedVariables.currentLanguage]);
		password.setTitle(getResources().getStringArray(R.array.password)[SharedVariables.currentLanguage]);
		password.setDialogTitle(getResources().getStringArray(R.array.password)[SharedVariables.currentLanguage]);
		blindUserMode.setTitle(getResources().getStringArray(R.array.blindUserMode)[SharedVariables.currentLanguage]);
		otherLanguages.setTitle(getResources().getStringArray(R.array.otherLanguagesTitle)[SharedVariables.currentLanguage]);
		otherLanguages.setSummary(getResources().getStringArray(R.array.otherLanguagesSummary)[SharedVariables.currentLanguage]);
	}
}
